<?php
namespace Core\Libs\Request;

class Request {

	private static $arrayRequests = array();
	private static $isDevelopment = FALSE;
	private static $isDebug = FALSE;
	private static $type = 0;
	private $key = NULL;

	function __construct() {

		$this->key = NULL;
	}

	final public function add() {

		self::$arrayRequests[] = array();

		return $this;
	}

	final public function addElement($arrayIdsRequests = array(), $value = NULL, $new = FALSE) {

		$stringToArray = '';

		if(is_string(&$arrayIdsRequests)) {

			$arrayIdsRequests =& preg_split('/\ *(=>|\.)\ */', &$arrayIdsRequests);
		}

		foreach($arrayIdsRequests as $idRequest) {

			$stringToArray .= '[\''.$idRequest.'\']';
		}

		if(empty($this->key)) {

			$this->key();
		}

		($new === TRUE) ? eval('self::$arrayRequests[$this->key]' . $stringToArray . '[] =& $value;') : eval('self::$arrayRequests[$this->key]' . $stringToArray . ' =& $value;');

		return $this;
	}

	final public function prev() {

		if(is_array(self::$arrayRequests) && count(self::$arrayRequests) > 0) {

			prev(self::$arrayRequests);
		}

		return $this;
	}

	final public function remove() {

		if(is_array(self::$arrayRequests) && count(self::$arrayRequests) > 0) {

			array_pop(self::$arrayRequests);
		}

		return $this;
	}

	final public function removeElement($arrayIdsRequests = array()) {

		$stringToArray = '';

		if(is_string(&$arrayIdsRequests)) {

			$arrayIdsRequests =& preg_split('/\ *(=>|\.)\ */', &$arrayIdsRequests);
		}

		foreach($arrayIdsRequests as $idRequest) {

			$stringToArray .= '[\''.$idRequest.'\']';
		}

		eval('unset(self::$arrayRequests[$this->current]' . $stringToArray . ');');
		self::$arrayRequests =& array_values(self::$arrayRequests);

		return $this;
	}

	final public function reset() {

		self::$arrayRequests = array();
		return $this;
	}

	final public function setType($type = 0) {

		if($this->length() < 1) {

			self::$type = $type;
		}

		return $this;
	}

	final public function getType($string = FALSE) {

		if($string === TRUE) {

			if(self::$type === 0) {

				return 'sync';
			}

			else {

				return 'assync';
			}
		}

		return self::$type;
	}

	final public function get($id = NULL) {

		if(!empty($id) && is_array(self::$arrayRequests) && array_key_exists($id, &self::$arrayRequests)) {

			return self::$arrayRequests[$id];
		}

		return NULL;
	}

	final public function getAll() {

		return self::$arrayRequests;
	}

	final public function getElement($arrayIdsRequests = array()) {

		$stringToArray = '';

		if(is_string(&$arrayIdsRequests)) {

			$arrayIdsRequests =& preg_split('/\ *(=>|\.)\ */', &$arrayIdsRequests);
		}

		foreach($arrayIdsRequests as $idRequest) {

			$stringToArray .= '[\''.$idRequest.'\']';
		}

		if(empty($this->key)) {

			$this->key();
		}

		$value = NULL;

		eval('$value = self::$arrayRequests[$this->key]' . $stringToArray . ';');

		return $value;
	}

	final public function getName() {

		return $this->getElement(array('name'));
	}

	final public function length() {

		return count(self::$arrayRequests);
	}

	private function key() {

		if(is_array(self::$arrayRequests) && count(self::$arrayRequests) > 0) {

			end(self::$arrayRequests);
			$this->key = key(self::$arrayRequests);
		}

		return $this;
	}
}
?>